--------------------------------------------------------------------------------
-- vGetNodeMaxSize
--
--
function vGetNodeMaxSize node = 
(
    local size = node.max - node.min
    
    local r = size.x
    
    if r < size.y do
    (
        r = size.y
    )
    
    if r < size.z do
    (
        r = size.z
    )
    
    return r
)

--------------------------------------------------------------------------------
-- vSaturate
--      Clamp a value between 0 and 1
--
-- @param val The value to be clamped
--
-- @return The clamped value
--
function vSaturate val =
(
    if ( classof val ) == Float do
    (
        if val > 1 do val = 1
        if val < 0 do val = 0
        
        return val
    )
    
    if ( classof val ) == Point2 do
    (
        if val.x > 1 do val.x = 1
        if val.x < 0 do val.x = 0
        
        if val.y > 1 do val.y = 1
        if val.y < 0 do val.y = 0
        
        return val
    )
    
    if ( classof val ) == Point3 do
    (
        if val.x > 1 do val.x = 1
        if val.x < 0 do val.x = 0
        
        if val.y > 1 do val.y = 1
        if val.y < 0 do val.y = 0
        
        if val.z > 1 do val.z = 1
        if val.z < 0 do val.z = 0
        
        return val
    )
    
    return val
)

--------------------------------------------------------------------------------
function vCalcTangentAndBinormal p0 p1 p2 uv0 uv1 uv2 =
(
    local e1 = p1 - p0
    local e2 = p2 - p0
    
    local dUV1 = uv1 - uv0
    local dUV2 = uv2 - uv0
    
    local det = ( dUV1.x * dUV2.y ) - ( dUV2.x * dUV1.y )
    
    local t = ( e1 * dUV2.y - e2 * dUV1.y ) / det
    local b = ( e2 * dUV1.x - e1 * dUV2.x ) / det
    
    local results = #()
    append results ( normalize t )
    append results ( normalize b )
    
    return results
    
)

--------------------------------------------------------------------------------
function vGammaExpand rgb =
(
    local gamma = 2.2
    
    return [ ( pow rgb.x gamma ), ( pow rgb.y gamma ), ( pow rgb.z gamma ) ] 
    
    --return rgb
)

function vGammaCompress rgb =
(
    local gamma = 0.45454545
    
    return [ ( pow rgb.x gamma ), ( pow rgb.y gamma ), ( pow rgb.z gamma ) ]
    
    --return rgb
)